<?php
	namespace app\api\controller;
	use think\Db;

	function test_input($data)
	{
		$data = trim($data); //去除用户输入数据中不必要的字符（多余的空格、制表符、换行）
		$data = stripslashes($data); //删除用户输入数据中的反斜杠（\）
		$data = htmlspecialchars($data); //把特殊字符转换为 HTML 实体
		return $data;
	}

	class Json
	{
		//连接测试首页 172.31.3.27:84/index.php?s=api/json
		public function index()
		{
			return json(['msg' => '测试连接成功']);
		}

		//传入工单号或传入工单号+金线+晶片信息
		//连接测试页 172.31.3.27:84/index.php?s=api/json/gd2json?gdh=工单号
		public function gd2json($gdh) //$RQ,$GD,$JT)
		{
			$gdh = test_input($gdh); //把自己下水刷一刷

			$KHB = ""; //客户别 (用于球径和球厚标准)
			$HGKHB = ""; //弧高用的客户别
			// $GDH = []; //数组用来存正则结果,一般是下标0,如果没0就报错
			$msg = "连接成功"; //返回的文本

			$p = 0; //晶片的计数

			$jxmx = ""; //金线明细
			$xc = ""; //线材
			$xcmil = ""; //线材密尔
			$xcmm = ""; //线材毫米
			$xccctj = ""; //线材尺寸条件

			$jpmx = array(); //晶片明细
			$jppum = ""; //晶片p微米
			$jpnum = ""; //晶片n微米
			$jpgd = ""; //晶片高度

			$HG = null; //弧高
			$HGHD = 0; //弧高标准的晶片厚度
			$HGXC = "金线/合金线/银线"; //弧高线材
			$CP = ""; //产品
			$CPLX = ""; //产品类型
			$CPXL = ""; //产品系列

			$ISGD = preg_match('/^([A-Z\d]{7,9})(?:\-[A-Z\d])?$/', $gdh, $GDH); //正则测试工单号
			
			if (!$ISGD)
			{
				$msg = "连接成功, 但工单号错误"; //返回的文本
				return $msg;
			}
			
			if(preg_match("/^(AF|AS|AD)[A-Z\d]{7}(\-[A-Z\d])?$/",$GDH[0])) //如果是LL/三星/CREE的工单号
			{
				$HGKHB = "LL/三星/CREE";
			}

			if(preg_match("/^AK[A-Z\d]{7}\-?[A-Z\d]?$/",$GDH[0])) //如果是KMS的工单号
			{
				$KHB = "KMS";
				$HGKHB = "KMS";
				$HGHD = 0;
			}
			else //否则就不是
			{
				$KHB = "非KMS";
				$HGKHB = "常规";
			}

			$JX = Db::table('mom_order') //先找到母工单表
				->alias('mh') //主表设置别名的方法
				->join(['mom_orderdetail' => 'md'], 'mh.MoId = md.MoId') //连接明细表
				->join(['mom_moallocate' => 'mdd'], 'md.MoDId = mdd.MoDId') //连接子件表
				->join(['Inventory' => 'i'], 'mdd.InvCode = i.cInvCode') //连接存货档案 Inventory
				->leftJoin(['金线尺寸档案' => 'x'], 'i.cInvName = x.系统品名') //左连接
				->leftJoin(['晶片尺寸档案' => 'j'], 'i.cInvName = j.CHIP') //左连接
				->whereLike('mh.MoCode', $GDH[1] . '%')
				->where(Db::raw("MID(mdd.InvCode,1,1) = 'F'"))
				// ->where(Db::raw("MID(mdd.InvCode,1,1) IN ('A','F')"))
				// ->column('md.InvCode, mdd.InvCode, i.cInvName, x.mil AS xcmil, x.mm AS xcmm, x.线材 AS xc, j.晶片高度 AS jpgd, j.Pμm AS jppum, j.Nμm AS jpnum')
				->column('md.InvCode AS CPXH, mdd.InvCode, i.cInvName, x.mil AS xcmil, x.mm AS xcmm, x.线材 AS xc')
			;

			$JP = Db::table('mom_order') //先找到母工单表
				->alias('mh') //主表设置别名的方法
				->join(['mom_orderdetail' => 'md'], 'mh.MoId = md.MoId') //连接明细表
				->join(['mom_moallocate' => 'mdd'], 'md.MoDId = mdd.MoDId') //连接子件表
				->join(['Inventory' => 'i'], 'mdd.InvCode = i.cInvCode') //连接存货档案 Inventory
				->leftJoin(['金线尺寸档案' => 'x'], 'i.cInvName = x.系统品名') //左连接
				->leftJoin(['晶片尺寸档案' => 'j'], 'i.cInvName = j.CHIP') //左连接
				->whereLike('mh.MoCode', $GDH[1] . '%')
				->where(Db::raw("MID(mdd.InvCode,1,1) = 'A'"))
				// ->where(Db::raw("MID(mdd.InvCode,1,1) IN ('A','F')"))
				// ->column('md.InvCode, mdd.InvCode, i.cInvName, x.mil AS xcmil, x.mm AS xcmm, x.线材 AS xc, j.晶片高度 AS jpgd, j.Pμm AS jppum, j.Nμm AS jpnum')
				->column('mdd.InvCode, i.cInvName, j.GD AS jpgd, j.P AS jppum, j.N AS jpnum')
			;
			// var_dump(count($JX),$JX);
			// var_dump(count($JP),$JP);
			// var_dump(count($JX),$JP);
			//var_dump($JX[$key]['xc']);
			// var_dump($JX);
			// var_dump($JP);
			if(count($JX)>0)
			{
				foreach($JX as $key => $val)
				{
					$jxmx =  $JX[$key]; //金线基本唯一,直接降维
					$xcmil = $JX[$key]['xcmil'];
					$xcmm = $JX[$key]['xcmm'];
					if($xcmil * 1 > 0)
					{
						$xccctj = "mil = '".$xcmil."'"; //线材尺寸条件
					}
					else if($xcmm * 1 > 0)
					{
						$xccctj = "mm = '".$xcmm."'";
					}

					if($HGKHB == 'KMS')
					{
						$HGXC = $JX[$key]['xc'];
					}

					if($JX[$key]['xc'] == '金线')
					{
						$xc = "'金线', '金线/合金线'";
					}
					else if($JX[$key]['xc'] == '合金线')
					{
						$xc = "'合金线', '金线/合金线'";
					}
					else //否则就银线或铜线
					{
						$xc = "'" . $JX[$key]['xc'] . "'";
					}
				}

				$CP = Db::table('waixingdaima')
					->where('WaiXingDaiMa',substr($jxmx['CPXH'],1,2))
					->select()
				;

				$QHQJ = Db::table('球厚球径标准')
					->where("客户别 = '" . $KHB . "'")
					->where("线材 IN (" . $xc . ")")
					->where($xccctj)
					->select()
				;

				// var_dump($QHQJ);
				$YHQH = ['L' => $QHQJ[0]['一焊球厚下限'], 'U' => $QHQJ[0]['一焊球厚上限']];
				$EHQJ = ['L' => $QHQJ[0]['二焊球径下限'], 'U' => $QHQJ[0]['二焊球径上限']];
				$EHQH = ['L' => $QHQJ[0]['二焊球厚下限'], 'U' => $QHQJ[0]['二焊球厚上限']];
			}
			else
			{
				$msg = "JX没有返回数据";
				$jxmx = "没有数据";
				$YHQH = "没有数据";
				$EHQJ = "没有数据";
				$EHQH = "没有数据";
				$HG = "没有数据";
			}

			//var_dump($JX[$key]['xc']);
			if(count($JP)>0)
			{
				foreach($JP as $key => $val)
				{
					$p += 1; //累加,然后继续循环
					$jpmx['JP'.$p] = $JP[$key]; //把晶片按傅鹏要求分开

					$jppum = $JP[$key]['jppum']; //晶片p微米
					$jpnum = $JP[$key]['jpnum']; //晶片n微米
					$jpgd = $JP[$key]['jpgd']; //晶片高度

					if($jpgd != null)
					{
						$HGHD = $jpgd; //弧高标准的晶片高度
					}

					// var_dump($jppum, $jpnum);
					//这里以下把PN结的数据也放进来
					$YHQJP = Db::table('一焊球径标准') //先找到母工单表
						->alias('QJ1') //主表设置别名的方法
						->where("QJ1.客户别 = '" . $KHB . "'")
						->where("QJ1.pad尺寸 = '" . $jppum . "'") //P结的pad尺寸
						->where("QJ1.线材 IN (" . $xc . ")") //线材
						->where($xccctj) //线材尺寸条件
						->field(['一焊球径下限' => 'L', '一焊球径上限' => 'U'])
						->select()
						// ->select("一焊球径下限 AS '0', 一焊球径上限 AS '1'")
					;

					$YHQJN = Db::table('一焊球径标准')
						->where("客户别 = '" . $KHB . "'")
						->where("pad尺寸 = '" . $jpnum . "'")
						->where("线材 IN (" . $xc . ")")
						->where($xccctj)
						->field(['一焊球径下限' => 'L', '一焊球径上限' => 'U'])
						->select()
					;

					// var_dump($JX,$JP,$YHQJP,$YHQJN);
					//$YHQJ = ['P' => $YHQJP[0], 'N' => $YHQJN[0]];
					if(count($YHQJP) > 0 && count($YHQJN) > 0)
					{
						$YHQJ['JP'.$p] = ['P' => $YHQJP[0], 'N' => $YHQJN[0]];
					}
					else
					{
						$YHQJ['JP'.$p] = "没有数据";
					}

					if(count($CP)>0)
					{
						foreach($CP as $key => $val)
						{
							$CPLX = $CPXL = $CP[$key]['ChanPinLeiXing']; //产品类型

							if($CP[$key]['WaiXingChiCun'] == '3030' || $CP[$key]['WaiXingChiCun'] == '2835')
							{
								$CPXL = $CP[$key]['WaiXingChiCun']; //产品系列
							}
							else
							{
								$CPXL = $CPLX; //产品系列 = 产品类型
							}
						}

						$HGDATA = Db::table('弧高标准')
							->where("客户别 = '" . $HGKHB . "'")
							->where("线材 IN (" . $xc . ")")
							->where("芯片厚度 = '" . $HGHD . "'")
							->where("产品类型 = '" . $CPLX . "'")
							->where("产品系列 = '" . $CPXL . "'")
							->field(['HGL' => 'L', 'HGU' => 'U'])
							->select()
						;
						//var_dump($HGKHB, $HGHD, $CPLX, $CPXL, $HGDATA);

						foreach($HGDATA as $key => $val)
						{
							$HG['JP'.$p] = $HGDATA[$key];
						}
					}
					else
					{
						$HG = "没有数据";
					}
				}
			}
			else
			{
				$msg = "JP没有返回数据";
				$YHQJ = "没有数据";
				$jpmx = "没有数据";
			}

			if($HGKHB == 'KMS' && $CPXL && $CPLX)
			{
				$HG = Db::table('弧高标准')
					->where("客户别 = '" . $HGKHB . "'")
					->where("线材 IN (" . $xc . ")")
					->where("产品类型 = '" . $CPLX . "'")
					->where("产品系列 = '" . $CPXL . "'")
					->field(['HGL' => 'L', 'HGU' => 'U'])
					->select()
				;

				foreach($HGDATA as $key => $val)
				{
					$HG['JP'.$p] = $HGDATA[$key];
				}
			}

			return json([
				'msg' => $msg, //返回的文本
				'KHB' => $KHB, //客户别
				'CP' => $CP[0], //产品
				'HGKHB' => $HGKHB, //弧高的客户别
				'JX' => $jxmx, //金线明细
				'JP' => $jpmx, //晶片明细
				'0' => $YHQJ, //PN结一焊球径
				'1' => $YHQH, //一焊球厚
				'2' => $EHQJ, //二焊球径 有问题
				'3' => $EHQH, //二焊球厚 有问题
				'4' => $HG[0], //弧高 没有出来
			]);
		}

		//连接测试页 172.31.3.27:84/index.php?s=api/chip?chip=晶片/晶片品名&gd=数字&p=数字&n=数字
		public function chip($chip,$gd,$p,$n) //$RQ,$GD,$JT)
		{
			$chip = test_input($chip); //把自己下水刷一刷
			$gd = test_input($gd); //把自己下水刷一刷
			$p = test_input($p); //把自己下水刷一刷
			$n = test_input($n); //把自己下水刷一刷
			if(preg_match("/^晶片\/.*?$/",$chip) && preg_match("/^\d+$/",$gd) && preg_match("/^\d+$/",$p) && preg_match("/^\d+$/",$n))
			{
				$data = ['CHIP' => $chip, 'GD' => $gd, 'P' => $p, 'N' => $n];
				$chipdata = Db::table('晶片尺寸档案')
					->where('CHIP', $chip)
					->select()
				;

				if(count($chipdata) > 0)
				{
					Db::table('晶片尺寸档案')
						->where('CHIP', $chip)
						->data($data)
						->update()
					;
					$msg = '更新成功';
				}
				else
				{
					Db::table('晶片尺寸档案')
						->insert($data)
					;
					$msg = '插入成功';
				}
			}
			else
			{
				$msg = '传进来的数据类型错误,请确认！<br/>晶片是否包含【晶片/】或高度和PN结pad尺寸是否为【数字】';
			}

			return $msg;
		}

		//连接测试页 http://127.0.0.1:85/index.php?s=/api/json/chipdata
		public function chipdata()
		{
			return json(
				Db::table('晶片尺寸档案')
					->select()
			);
		}
	}
